package com.woniu.controller;


import com.woniu.entity.Units;
import com.woniu.service.IUnitsService;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
//单元信息
@RestController
public class UnitsController {
    @Autowired
    private IUnitsService unitService;

    /**
     * 新增
     */
    @PostMapping("/units/save")
    private ResultVO save(@RequestBody Units units) {
        boolean falg = false;
        try {
            falg = unitService.save(units);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (falg ? ResultVO.success() : ResultVO.failure());

    }

    /**
     * 删除一个
     */
    @PostMapping("/units/deleteById")
    private ResultVO deleteById(@RequestBody String uid) {
        int rows = 0;
        try {
            rows = unitService.deleteById(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (rows > 0 ? ResultVO.success() : ResultVO.failure());
    }

    /**
     * 修改
     */
    @PostMapping("/units/updateById")
    private ResultVO updateByIds(@RequestBody Units units) {
        boolean falg = false;
        try {
            falg = unitService.updateById(units);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (falg ? ResultVO.success() : ResultVO.failure());
    }

    /**
     * 查询全部
     */
    @PostMapping("/untis/findAll")
    private ResultVO findAll() {
        List<Units> list = null;
        try {
            list = unitService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return ResultVO.success(list);
    }

    /**
     * 根据id查询一个
     */
    @PostMapping("/untis/findById")
    private ResultVO findById(@RequestBody String uid) {
        List<Units> list = null;
        try {
            list = unitService.findById(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return ResultVO.success(list);

    }

    /**
     * 根据楼号查询单元信息
     * */
    @PostMapping("/untis/findBybuildingId")
    private ResultVO findBybuildingId(@RequestBody String bid){
        System.out.println(bid);
        List<Units> list=null;
        try {
            list=unitService.findByBid(bid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return ResultVO.success(list);
    }


    /**
     * 查询全部去重
     */
    @PostMapping("/untis/find")
    private ResultVO find() {
        List<Units> list = null;
        try {
            list = unitService.find();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return ResultVO.success(list);
    }


}

